package com.gaogzhen.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.supercsv.cellprocessor.CellProcessorAdaptor;
import org.supercsv.util.CsvContext;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;

/**
 * supercsv单元格格式化器
 * @author Administrator
 * @version 1.0
 * @description
 * @date 2022-11-21 11:22
 */
public class CellProcessorAdaptorUtils {

    public static CellProcessorAdaptor long2StringAdaptor() {
        return new CellProcessorAdaptor() {
            @Override
            public <T> T execute(Object o, CsvContext csvContext) {
                return (T) String.valueOf(o);
            }
        };
    }

    public static CellProcessorAdaptor moneyAdaptor() {
        DecimalFormat decimalFormat = new DecimalFormat("0.00000000");
        return  new CellProcessorAdaptor() {
            @Override
            public <T> T execute(Object o, CsvContext csvContext) {
                BigDecimal s = (BigDecimal) o;
                String money = decimalFormat.format(s);
                return (T) money;
            }
        };
    }

    public static CellProcessorAdaptor payTypeAdaptor() {
        return new CellProcessorAdaptor() {
            @Override
            public <T> T execute(Object o, CsvContext csvContext) {
                String s = String.valueOf(o);
                String type = null;
                switch (s) {
                    case "alipay":
                        type = "支付宝";
                        break;
                    case "cai1pay":
                        type = "财易付";
                        break;
                    case "bank":
                        type = "银联";
                        break;
                    case "linepay":
                        type = "在线支付";
                        break;
                    default:
                        type = "";
                        break;
                }
                return (T) type;
            }
        };
    }

    public static CellProcessorAdaptor timeAdaptor(String pattern) {
        final String[] pattern1 = {pattern};
        return new CellProcessorAdaptor() {
            @Override
            public <T> T execute(Object o, CsvContext csvContext) {
                Date date  = (Date) o;
                if (StringUtils.isEmpty(pattern1[0])) {
                    pattern1[0] = "yyyy-MM-dd HH:mm:ss";
                }
                return (T) DateFormatUtils.format(date, pattern1[0]);
            }
        };
    }

    public static CellProcessorAdaptor statusAdaptor(String[] status) {
        return new CellProcessorAdaptor() {
            @Override
            public <T> T execute(Object o, CsvContext csvContext) {
                return (T) status[(Byte)o];
            }
        };
    }
}
